package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BibliotecaApp {

    public static void main(String[] args) {
        displayWelcomeMSG();
        BookList bookList = new BookList();
        listCommands();
        listenForCommands(bookList);
    }

    public static void listenForCommands(BookList bookList) {
        while (true) {
            try {
                BufferedReader c = new BufferedReader(new InputStreamReader(System.in));
                String command = c.readLine();
                if (command.contains("help")) {
                    listCommands();
                } else if (command.contains("list")) {
                    listBooks(bookList);
                } else if (command.contains("checkout")) {
                    Book bookToCheckOut = bookList.findBook(Integer.parseInt(command.split(" ")[1]));
                    bookToCheckOut.checkoutBook();
                } else if (command.contains("desc")) {
                    Book book = bookList.findBook(Integer.parseInt(command.split(" ")[1]));
                    book.displayInfo();
                } else if (command.contains("return")) {
                    Book book = bookList.findBook(Integer.parseInt(command.split(" ")[1]));
                    book.returnBook();
                } else if (command.contains("quit")) {
                    break;
                } else {
                    System.out.println("Select a valid option!");
                    System.out.println("Type help for the commands list");
                }
            } catch (IOException e) {
                System.out.println("ERROR!!!!");
            }

        }
    }

    public static void displayWelcomeMSG() {
        System.out.println("Welcome to Biblioteca! The library that comes to you!");
    }

    public static void listBooks(BookList bookList) {
        bookList.displayBooks();
    }

    public static void listCommands() {
        System.out.println("Bibiloteca Commands:");
        System.out.println("help        \t\t Lists commands");
        System.out.println("quit        \t\t Quits Biblioteca");
        System.out.println("list        \t\t Lists books");
        System.out.println("checkout :id\t\t Checksout the book with that id");
        System.out.println("return :id  \t\t Returns the book with that id");
        System.out.println("desc :id    \t\t Displays book details with that id");
    }
}
